package designpattern.command.llm.model;

public class StringBuf {
    private StringBuffer buffer = new StringBuffer();
    
    public void append(String text) {
        buffer.append(text);
    }
    
    public void insert(int position, String text) {
        if (position < 0 || position > buffer.length()) {
            throw new IllegalArgumentException("插入位置必须在 0 到 " + buffer.length() + " 之间");
        }
        buffer.insert(position, text);
    }
    
    public String delete(int position, int length) {
        int endPos = position + length;
        if (position < 0 || endPos > buffer.length()) {
            throw new IllegalArgumentException("删除范围必须在 0 到 " + buffer.length() + " 之间");
        }
        String deletedText = buffer.substring(position, endPos);
        buffer.delete(position, endPos);
        return deletedText;
    }
    
    public String getContent() {
        return buffer.toString();
    }
    
    public int length() {
        return buffer.length();
    }
}